//****************************************************************
//* Copyright (c) 2008 Ford Motor Company. All Rights Reserved.
//*
//* $$Workfile:   CurrencyLogic.java  $$
//* $$Revision:   1.3  $$
//* $$Author:   aponte  $$
//* $$Date:   Dec 23 2008 11:19:40  $$
//*
//*****************************************************************
package ar.com.ford.it.fullEconomics.logic;

import java.sql.Timestamp;
import java.util.Collection;
import org.apache.commons.lang.StringUtils;
import ar.com.ford.it.fullEconomics.FullEconomicsPrs;
import ar.com.ford.it.fullEconomics.exception.FullEconomicsException;
import ar.com.ford.it.fullEconomics.persistence.dao.CurrencyDao;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesm01Currency;
import ar.com.ford.it.fullEconomics.wslx.FullEconomicsWslxUser;
import ar.com.ford.it.webengine.persistence.PersistenceUtil;
import com.ford.it.logging.ILogger;
import com.ford.it.logging.Level;
import com.ford.it.logging.LogFactory;
import com.ford.it.persistence.PersistenceException;

public class CurrencyLogic {

    private static final String CLASS_NAME = CurrencyLogic.class.getName();
    private static final ILogger log = LogFactory.getInstance().getLogger(
            CLASS_NAME);

    /**
     * Obtiene todas las monedas existentes
     * 
     * @return
     * @throws FullEconomicsException 
     */
    public Collection<Teesm01Currency> findAllCurrency() throws FullEconomicsException {

        String METHOD_NAME = "findAllCurrency";
        log.entering(CLASS_NAME, METHOD_NAME);

        Collection<Teesm01Currency> resCol = null;
        CurrencyDao currDao = new CurrencyDao();
        try {
            resCol = currDao.findAllCurrency();
        } catch (PersistenceException e) {
            log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME, "Find All Currencies Exception", e);
            throw new FullEconomicsException(e,FullEconomicsPrs.MessageErrors.Currencys.FINDALL);
        }

        log.exiting(CLASS_NAME, METHOD_NAME);

        return resCol;
    }

    /**
     * Obtiene las monedas por codigo
     * 
     * @param currenCode
     * @return
     */
    public Teesm01Currency findCurrencyByCode(String currenCode)  throws FullEconomicsException {

        String METHOD_NAME = "findCurrencyByCode";
        log.entering(CLASS_NAME, METHOD_NAME);

        Teesm01Currency result = new Teesm01Currency();
        CurrencyDao currDao = new CurrencyDao();
        try {
            result = currDao.findCurrencyByCode(currenCode);
        } catch (PersistenceException e) {
            log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME, "Find Currency By Code Exception", e);
            throw new FullEconomicsException(e,FullEconomicsPrs.MessageErrors.Currencys.FINDBYCODE);
        }

        log.exiting(CLASS_NAME, METHOD_NAME);

        return result;
    }

    /**
     * Busca Currencys segun code
     * 
     * @param code
     * @return Currencys que se correspondan con la busqueda.
     */
    public Collection<Teesm01Currency> findCurrencies(String code) throws FullEconomicsException  {

        String METHOD_NAME = "findCurrencies";
        log.entering(CLASS_NAME, METHOD_NAME);

        CurrencyDao dao = new CurrencyDao();
        Teesm01Currency currency = new Teesm01Currency();
        Collection<Teesm01Currency> ret = null;

        if (StringUtils.isBlank(code)) {
            currency.setEesm01CurrencyCodeC(null);
        } else {
            currency.setEesm01CurrencyCodeC(code);
        }

        try {
            ret = dao.findCurrencys(currency);
        } catch (PersistenceException e) {
            log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME, "Find Currencies Exception", e);
            throw new FullEconomicsException(e,FullEconomicsPrs.MessageErrors.Currencys.FINDALL);
        }

        log.exiting(CLASS_NAME, METHOD_NAME);

        return ret;
    }

    /**
     * Da de alta el Currency pasado por parametro
     * 
     * @param currency
     * Currency a dar de alta
     * @param user
     * Usuario que da de alta el Currency
     */
    public void createCurrency(Teesm01Currency currency,
                                  FullEconomicsWslxUser user) throws FullEconomicsException  {

        String METHOD_NAME = "createCurrency";
        log.entering(CLASS_NAME, METHOD_NAME);

        CurrencyDao dao = new CurrencyDao();

        try {

            currency.setEesm01CreateUserC(user.getId());
            currency.setEesm01LastUpdtUserC(user.getId());
            currency
                    .setEesm01CreateS(new Timestamp(System.currentTimeMillis()));
            currency.setEesm01LastUpdtS(new Timestamp(System
                    .currentTimeMillis()));
            
            currency.setEesm01CurrencyInDateY(currency.getEesm01CurrencyInDateY());
            currency.setEesm01CurrencyOutDateY(currency.getEesm01CurrencyOutDateY());

            PersistenceUtil.newTransaction();
            
            dao.createCurrency(currency);
            PersistenceUtil.commitTransaction();
        
        } catch (PersistenceException pe) {

            log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME,
                    "PersistenceException", pe);
            try {
                PersistenceUtil.rollbackTransaction();
            } catch (PersistenceException rollbackEx) {
                rollbackEx.printStackTrace();
            }
            
            log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME, "Create Currency Exception", pe);
            throw new FullEconomicsException(pe,FullEconomicsPrs.MessageErrors.Currencys.CREATE);
        }

        log.exiting(CLASS_NAME, METHOD_NAME);
    }

    /**
     * Actualiza el Currency pasado por parametro
     * 
     * @param currency
     * Currency a actualizar
     * @param user
     * Usuario que actualiza el Currency
     */
    public void updateCurrency(Teesm01Currency currency,
                                  FullEconomicsWslxUser user)  throws FullEconomicsException {

        String METHOD_NAME = "updateCurrency";
        log.entering(CLASS_NAME, METHOD_NAME);

        CurrencyDao dao = new CurrencyDao();

        Teesm01Currency workingCopy = null;

        try {
            PersistenceUtil.newTransaction();

            PersistenceUtil.getPersistenceManager().clearCache();

            workingCopy = dao.getCurrencyForUpdate(currency
                    .getEesm01CurrencyCodeC());
            workingCopy.setEesm01LastUpdtUserC(user.getId());
            workingCopy.setEesm01LastUpdtS(new Timestamp(System
                    .currentTimeMillis()));

            // Campos modificados, in y out date
            workingCopy.setEesm01CurrencyInDateY(currency
                    .getEesm01CurrencyInDateY());
            workingCopy.setEesm01CurrencyOutDateY(currency
                    .getEesm01CurrencyOutDateY());

            PersistenceUtil.commitTransaction();

        } catch (PersistenceException pe) {

            try {
                PersistenceUtil.rollbackTransaction();
            } catch (PersistenceException rollbackEx) {
                rollbackEx.printStackTrace();
            }
            
            log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME, "Update Currency Exception", pe);
            throw new FullEconomicsException(pe,FullEconomicsPrs.MessageErrors.Currencys.UPDATE);
        }

        log.exiting(CLASS_NAME, METHOD_NAME);
    }

    /**
     * Elimina el ecnomicSource pasado por parametro
     * 
     * @param currency
     * Currency a eliminar
     */
    public void deleteCurrency(Teesm01Currency currency)  throws FullEconomicsException {

        String METHOD_NAME = "deleteCurrency";
        log.entering(CLASS_NAME, METHOD_NAME);

        try {
            PersistenceUtil.newTransaction();

            CurrencyDao dao = new CurrencyDao();
            
            dao.deleteCurrency(currency);
            PersistenceUtil.commitTransaction();

        } catch (PersistenceException pe) {

            log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME, "Create Currency Exception", pe);
            throw new FullEconomicsException(pe,FullEconomicsPrs.MessageErrors.Currencys.DELETE);

        }

        log.exiting(CLASS_NAME, METHOD_NAME);
    }

}
